<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app" style="padding:10px;">
      <div style="border:5px solid red;">
        <p>parentComponent.number:{{number}}</p>
        <button @click="add">parentComponent+</button>
      </div>
      <child-component :count="number"></child-component>
    </div>
    <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
    <script>
      //在父组件里定义绑定数据到子组件
      var parentComponent = new Vue({
        el: "#app",
        data: () => ({ number: 200 }), //定义要传递给子组件属性
        methods: {
          add() {
            this.number++;
          },
        },
        beforeUpdate() {
          console.log("parentComponent.beforeUpdate");
        },
        updated() {
          console.log("parentComponent.updated");
        },
        components: {
          "child-component": {
            //属性可以定义对象，key是属性名,值 可以是单类型,可以是类型数组,还可以必填,设置默认值
            props: {
              count:{
                type:Number,//设置校验类型，必须是Number
                required:true,//是否必填
                default:0,//默认值
                //Invalid prop: custom validator check failed for prop "count". 自定义较验规则
                validator(value){
                  return value<100;//如果返回了false,说明校验不通过，出现警告
                }
              }
            }, //1.在子组件中声明子组件要接收的属性名。
            methods: {
              increase() {
                this.count++;
              },
            },
            template: `<div style="border:5px solid green"><button @click="increase">{{count}}</button></div>`,
            beforeUpdate() {
              console.log("childComponent.beforeUpdate");
            },
            updated() {
              console.log("childComponent.updated");
            },
          },
        },
      });
    </script>
  </body>
</html>
Avoid mutating a prop directly since the value will be overwritten whenever the
parent component re-renders.
